package cn.yuhh.api.v1.user;

import cn.yuhh.api.exception.ApiException;
import cn.yuhh.api.helper.ApiResponse;
import cn.yuhh.api.service.AbstractApiService;
import cn.yuhh.core.annotation.NotNull;
import cn.yuhh.core.domain.User;
import cn.yuhh.core.type.ResponseHeader;
import com.magic.spring.mongo.IMongoUpdate;
import com.magic.spring.mongo.Mongo;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author ysw
 * @create 2020-02-18 15:34
 * @descreption
 **/
@Component("userInfoModify1.0")
@NotNull(name = {"id","tel","password","confirmPassword"})
public class UserInfoMondifyService extends AbstractApiService {
    @Override
    public Map service(Map request) {
        String password = string(request, "password");
        String confirmPassword = string(request, "confirmPassword");
        if(!confirmPassword.equals(password)){
            throw new ApiException(ResponseHeader.CONFIRM_PASSWORD_ERROR);
        }
        Mongo.buildMongo().eq("_id",new ObjectId(id(request))).updateFirst(new IMongoUpdate() {
            @Override
            public void update(Update update) {
                update.set("tel", string(request, "tel"));
                update.set("password", string(request, "password"));
            }
        }, User.class);
        return ApiResponse.success();
    }
}